package com.UI
{
	import com.*;
	import com.Engine.*;
	import com.Home.*;
	import com.Packet.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.external.*;
	import flash.filters.*;
	import flash.geom.*;
	import flash.system.*;
	import flash.text.*;
	import flash.ui.*;
	import flash.utils.*;
	
	import gs.*;
	import gs.easing.*;
	import gs.events.TweenEvent;
	
	import org.aswing.*;
	import org.aswing.border.*;
	import org.aswing.border.EmptyBorder;
	import org.aswing.colorchooser.*;
	import org.aswing.dnd.*;
	import org.aswing.event.*;
	import org.aswing.ext.*;
	import org.aswing.geom.*;
	import org.aswing.tree.AbstractLayoutCache;
	
	public class FrameBag extends JFrame
	{
		//members define
		private var panelEquips:JPanel;
		private var panelImage:JPanel;
		private var btn_equip_hat:PanelItem
		private var btn_equip_cloth:PanelItem;
		private var btn_equip_belt:PanelItem;
		private var btn_equip_weapon:PanelItem;
		private var btn_equip_pants:PanelItem;
		private var btn_equip_shoe:PanelItem;
		private var panelTabs:JPanel;
		private var btn_bag_tab_all:JToggleButton;
		private var btn_bag_tab_equip:JToggleButton;
		private var btn_bag_tab_pet:JToggleButton;
		private var btn_bag_tab_props:JToggleButton;
		private var btn_bag_tab_task:JToggleButton;
		private var panelButtons:JPanel;
		private var btn_bag_sort:JButton;
		private var btn_bag_stall:JButton;
		private var panelItems:JPanel;
		private var panelPages:JPanel;
		private var btn_bag_page_1:JToggleButton;
		private var btn_bag_page_2:JToggleButton;
		private var btn_bag_page_3:JToggleButton;
		private var btn_bag_page_4:JToggleButton;
		private var btn_bag_page_5:JToggleButton;
		private var labelMoney:JLabel;
		private var labelIngot:JLabel;
		private var labelWood:JLabel;
		private var labelFood:JLabel;
		
		private var btn_close:JButton;
		public var _items:Array = new Array;
		private var _itemsEquip:Array = new Array;
		private var _btnsPages:Array = new Array;
		//private var _tooltipsItem:Array = new Array;
		//private var _tooltipsEquip:Array = new Array;
		
		public function FrameBag()
		{
			setResizable(false);
			setBorder(new EmptyBorder());
			
			btn_close = new JButton();
			btn_close.setLocation(new IntPoint(198, 0));
			btn_close.setSize(new IntDimension(27, 27));
			btn_close.setPreferredSize(new IntDimension(27, 27));
			this.getTitleBar().setCloseButton(btn_close);
			this.setClipMasked(false);
			getContentPane().setClipMasked(false);
			getTitleBar().getFrame().setClipMasked(false);
			btn_close.setClipMasked(false);
			getTitleBar().setMinimizeHeight(32);
			
			//component creation
			setSize(new IntDimension(278, 399));
			setPreferredSize(new IntDimension(278, 399));
			var layout0:EmptyLayout = new EmptyLayout();
			getContentPane().setLayout(layout0);
			
			panelEquips = new JPanel();
			panelEquips.setLocation(new IntPoint(0, 0));
			panelEquips.setSize(new IntDimension(278, 138));
			panelEquips.setPreferredSize(new IntDimension(278, 138));
			var layout1:EmptyLayout = new EmptyLayout();
			panelEquips.setLayout(layout1);
			
			panelImage = new JPanel();
			panelImage.setLocation(new IntPoint(88, 3));
			panelImage.setSize(new IntDimension(102, 130));
			panelImage.setPreferredSize(new IntDimension(102, 130));
			
			btn_equip_hat = new PanelItem();
			btn_equip_hat.setLocation(new IntPoint(27, 1));
			btn_equip_hat.setSize(new IntDimension(45, 45));
			btn_equip_hat.setPreferredSize(new IntDimension(45, 45));
			//btn_equip_hat.setText("label");
			
			btn_equip_cloth = new PanelItem();
			btn_equip_cloth.setLocation(new IntPoint(207, 1));
			btn_equip_cloth.setSize(new IntDimension(45, 45));
			btn_equip_cloth.setPreferredSize(new IntDimension(45, 45));
			//btn_equip_cloth.setText("label");
			
			btn_equip_belt = new PanelItem();
			btn_equip_belt.setLocation(new IntPoint(27, 46));
			btn_equip_belt.setSize(new IntDimension(45, 45));
			btn_equip_belt.setPreferredSize(new IntDimension(45, 45));
			//btn_equip_belt.setText("label");
			
			btn_equip_weapon = new PanelItem();
			btn_equip_weapon.setLocation(new IntPoint(207, 46));
			btn_equip_weapon.setSize(new IntDimension(45, 45));
			btn_equip_weapon.setPreferredSize(new IntDimension(45, 45));
			//btn_equip_weapon.setText("label");
			
			btn_equip_pants = new PanelItem();
			btn_equip_pants.setLocation(new IntPoint(27, 91));
			btn_equip_pants.setSize(new IntDimension(45, 45));
			btn_equip_pants.setPreferredSize(new IntDimension(45, 45));
			//btn_equip_pants.setText("label");
			
			btn_equip_shoe = new PanelItem();
			btn_equip_shoe.setLocation(new IntPoint(207, 91));
			btn_equip_shoe.setSize(new IntDimension(45, 45));
			btn_equip_shoe.setPreferredSize(new IntDimension(45, 45));
			//btn_equip_shoe.setText("label");
			
			panelTabs = new JPanel();
			panelTabs.setLocation(new IntPoint(0, 141));
			panelTabs.setSize(new IntDimension(278, 23));
			panelTabs.setPreferredSize(new IntDimension(278, 23));
			var layout2:FlowLayout = new FlowLayout();
			layout2.setAlignment(AsWingConstants.CENTER);
			layout2.setHgap(2);
			layout2.setVgap(0);
			panelTabs.setLayout(layout2);
			
			btn_bag_tab_all = new JToggleButton();
			btn_bag_tab_all.setLocation(new IntPoint(52, 4));
			btn_bag_tab_all.setSize(new IntDimension(42, 29));
			btn_bag_tab_all.setPreferredSize(new IntDimension(42, 23));
			btn_bag_tab_all.setText("label");
			
			btn_bag_tab_equip = new JToggleButton();
			btn_bag_tab_equip.setLocation(new IntPoint(79, 4));
			btn_bag_tab_equip.setSize(new IntDimension(31, 22));
			btn_bag_tab_equip.setText("label");
			
			btn_bag_tab_pet = new JToggleButton();
			btn_bag_tab_pet.setLocation(new IntPoint(159, -2));
			btn_bag_tab_pet.setSize(new IntDimension(42, 29));
			btn_bag_tab_pet.setPreferredSize(new IntDimension(42, 23));
			btn_bag_tab_pet.setText("label");
			
			btn_bag_tab_props = new JToggleButton();
			btn_bag_tab_props.setLocation(new IntPoint(98, 3));
			btn_bag_tab_props.setSize(new IntDimension(42, 29));
			btn_bag_tab_props.setPreferredSize(new IntDimension(42, 23));
			btn_bag_tab_props.setText("label");
			
			btn_bag_tab_task = new JToggleButton();
			btn_bag_tab_task.setLocation(new IntPoint(25, 19));
			btn_bag_tab_task.setSize(new IntDimension(42, 29));
			btn_bag_tab_task.setPreferredSize(new IntDimension(42, 23));
			btn_bag_tab_task.setText("label");
			
			panelButtons = new JPanel();
			panelButtons.setLocation(new IntPoint(9, 164));
			panelButtons.setSize(new IntDimension(19, 125));
			panelButtons.setPreferredSize(new IntDimension(19, 125));
			var layout3:FlowWrapLayout = new FlowWrapLayout();
			layout3.setPreferWidth(200);
			layout3.setAlignment(AsWingConstants.LEFT);
			layout3.setHgap(0);
			layout3.setVgap(2);
			panelButtons.setLayout(layout3);
			
			btn_bag_sort = new JButton();
			btn_bag_sort.setLocation(new IntPoint(5, 5));
			btn_bag_sort.setSize(new IntDimension(19, 23));
			btn_bag_sort.setPreferredSize(new IntDimension(19, 23));
			btn_bag_sort.setToolTipText("整理");
			btn_bag_sort.setText("label");
			
			btn_bag_stall = new JButton();
			btn_bag_stall.setLocation(new IntPoint(0, 27));
			btn_bag_stall.setSize(new IntDimension(19, 23));
			btn_bag_stall.setPreferredSize(new IntDimension(19, 23));
			btn_bag_stall.setToolTipText("摊位");
			btn_bag_stall.setText("label");
			
			panelItems = new JPanel();
			panelItems.setLocation(new IntPoint(26, 164));
			panelItems.setSize(new IntDimension(225, 180));
			panelItems.setPreferredSize(new IntDimension(225, 180));
			var layout4:GridLayout = new GridLayout();
			layout4.setRows(4);
			layout4.setColumns(5);
			layout4.setHgap(0);
			layout4.setVgap(0);
			panelItems.setLayout(layout4);
			
			panelPages = new JPanel();
			panelPages.setLocation(new IntPoint(252, 164));
			panelPages.setSize(new IntDimension(19, 125));
			panelPages.setPreferredSize(new IntDimension(19, 125));
			var layout5:GridLayout = new GridLayout();
			layout5.setRows(5);
			layout5.setColumns(1);
			layout5.setHgap(0);
			layout5.setVgap(2);
			panelPages.setLayout(layout5);
			
			btn_bag_page_1 = new JToggleButton();
			btn_bag_page_1.setLocation(new IntPoint(5, 5));
			btn_bag_page_1.setSize(new IntDimension(31, 22));
			btn_bag_page_1.setPreferredSize(new IntDimension(19, 23));
			btn_bag_page_1.setText("label");
			
			btn_bag_page_2 = new JToggleButton();
			btn_bag_page_2.setLocation(new IntPoint(5, 33));
			btn_bag_page_2.setSize(new IntDimension(19, 23));
			btn_bag_page_2.setText("label");
			
			btn_bag_page_3 = new JToggleButton();
			btn_bag_page_3.setLocation(new IntPoint(5, 59));
			btn_bag_page_3.setSize(new IntDimension(31, 22));
			btn_bag_page_3.setText("label");
			
			btn_bag_page_4 = new JToggleButton();
			btn_bag_page_4.setLocation(new IntPoint(5, 86));
			btn_bag_page_4.setSize(new IntDimension(31, 22));
			btn_bag_page_4.setText("label");
			
			btn_bag_page_5 = new JToggleButton();
			btn_bag_page_5.setLocation(new IntPoint(5, 113));
			btn_bag_page_5.setSize(new IntDimension(31, 22));
			btn_bag_page_5.setText("label");
			
			labelMoney = new JLabel();
			labelMoney.setLocation(new IntPoint(175, 346));
			labelMoney.setSize(new IntDimension(72, 14));
			labelMoney.setPreferredSize(new IntDimension(72, 14));
			labelMoney.setText("label");
			
			labelIngot = new JLabel();
			labelIngot.setLocation(new IntPoint(58, 346));
			labelIngot.setSize(new IntDimension(72, 14));
			labelIngot.setPreferredSize(new IntDimension(72, 14));
			labelIngot.setText("label");
			
			labelWood = new JLabel();
			labelWood.setLocation(new IntPoint(58, 366));
			labelWood.setSize(new IntDimension(72, 14));
			labelWood.setPreferredSize(new IntDimension(72, 14));
			labelWood.setText("label");
			
			labelFood = new JLabel();
			labelFood.setLocation(new IntPoint(175, 366));
			labelFood.setSize(new IntDimension(72, 14));
			labelFood.setPreferredSize(new IntDimension(72, 14));
			labelFood.setText("label");
			
			//component layoution
			getContentPane().append(panelEquips);
			getContentPane().append(panelTabs);
			getContentPane().append(panelButtons);
			getContentPane().append(panelItems);
			getContentPane().append(panelPages);
			getContentPane().append(labelMoney);
			getContentPane().append(labelIngot);
			getContentPane().append(labelWood);
			getContentPane().append(labelFood);
			
			panelEquips.append(panelImage);
			panelEquips.append(btn_equip_hat);
			panelEquips.append(btn_equip_cloth);
			panelEquips.append(btn_equip_belt);
			panelEquips.append(btn_equip_weapon);
			panelEquips.append(btn_equip_pants);
			panelEquips.append(btn_equip_shoe);
			
			panelTabs.append(btn_bag_tab_all);
			panelTabs.append(btn_bag_tab_equip);
			panelTabs.append(btn_bag_tab_pet);
			panelTabs.append(btn_bag_tab_props);
			panelTabs.append(btn_bag_tab_task);
			
			panelButtons.append(btn_bag_sort);
			panelButtons.append(btn_bag_stall);
			
			panelPages.append(btn_bag_page_1);
			panelPages.append(btn_bag_page_2);
			panelPages.append(btn_bag_page_3);
			panelPages.append(btn_bag_page_4);
			panelPages.append(btn_bag_page_5);
			
			//----------------------------------
			
			for(var i:int = 0; i < _itemsPerPage; i++)
			{
				var item:PanelItem = new PanelItem;
				panelItems.append(item);
				_items.push(item);
			}
			_itemsEquip.push(btn_equip_hat);
			_itemsEquip.push(btn_equip_cloth);
			_itemsEquip.push(btn_equip_belt);
			_itemsEquip.push(btn_equip_weapon);
			_itemsEquip.push(btn_equip_pants);
			_itemsEquip.push(btn_equip_shoe);
			
			_btnsPages.push(btn_bag_page_1);
			_btnsPages.push(btn_bag_page_2);
			_btnsPages.push(btn_bag_page_3);
			_btnsPages.push(btn_bag_page_4);
			_btnsPages.push(btn_bag_page_5);
			
			setImages();
			setData();
			
			addEventListener(MouseEvent.CLICK, onClick);
			addEventListener(FrameEvent.FRAME_CLOSING,onFrameClosing);
			addEventListener(AWEvent.HIDDEN,onFrameClosing);
			getContentPane().bringToTop(panelEquips);
			getContentPane().bringToTop(panelItems);
			panelEquips.setClipMasked(false);
			panelItems.setClipMasked(false);
			btn_bag_tab_all.addActionListener(onTabAll);
			btn_bag_tab_equip.addActionListener(onTabEquip);
			btn_bag_tab_pet.addActionListener(onTabPet);
			btn_bag_tab_props.addActionListener(onTabProps);
			btn_bag_tab_task.addActionListener(onTabTask);
		}
		private function onClick(e:Event):void
		{
			//PanelItem.hideClick();
		}
		private function onTabAll(e:Event):void
		{
			setData();
		}
		private function onTabEquip(e:Event):void
		{
			setData();
		}
		private function onTabPet(e:Event):void
		{
			setData();
		}
		private function onTabProps(e:Event):void
		{
			setData();
		}
		private function onTabTask(e:Event):void
		{
			setData();
		}
		private function onFrameClosing(e:Event):void
		{
			if(FrameTrade.isShowing())
				FrameTrade.hide();
			if(_needCloseChar)
				FrameChar.hide();
			_needCloseChar = false;
			PanelItem.hideClick();
		}
				

		private var _itemsPerPage:int = 20;
		public function setImages():void
		{
			//getContentPane().append(labelMoney);
			//getContentPane().append(labelIngot);
			//getContentPane().append(labelWood);
			//getContentPane().append(labelFood);
			
			panelImage.setClipMasked(false);
			
			var filter:GlowFilter;
			var font:ASFont = new ASFont("宋体", 12, false, false, false, false);
			filter = new GlowFilter(0x000000, 1, 2, 2, 10);
			labelMoney.setFont(font);
			labelMoney.setForeground(ASColor.WHITE);
			labelMoney.setTextFilters(new Array(filter));
			labelIngot.setFont(font);
			labelIngot.setForeground(ASColor.WHITE);
			labelIngot.setTextFilters(new Array(filter));
			labelWood.setFont(font);
			labelWood.setForeground(ASColor.WHITE);
			labelWood.setTextFilters(new Array(filter));
			labelFood.setFont(font);
			labelFood.setForeground(ASColor.WHITE);
			labelFood.setTextFilters(new Array(filter));
			
			var img_char_background:DisplayObject = Client.getUIElement("img_bag_bg") as DisplayObject
			setBackgroundDecorator(new AssetBackground(img_char_background));
			setSize(new IntDimension(img_char_background.width, img_char_background.height));
			setPreferredSize(new IntDimension(img_char_background.width, img_char_background.height));
			//pack();
			//setLocationXY((Engine.W - img_char_background.width)/(FrameTrade.isShowing()?1:2),(Engine.H - img_char_background.height)/2);

			
			btn_close.wrapSimpleButton(Client.getUIElement("btn_close") as SimpleButton);
			
			btn_bag_stall.addActionListener(function ():void{FrameStall.show(CharInfo.m_arStallItems,_frame);});
			
			btn_equip_hat.icon_id = 0;
			btn_equip_belt.icon_id = 0;
			btn_equip_cloth.icon_id = 0;
			btn_equip_pants.icon_id = 0;
			btn_equip_shoe.icon_id = 0;
			btn_equip_weapon.icon_id = 0;
			
			var item:PanelItem;
			var nIndex:uint = 0;
			for each (item in _items)
			{
				item.addEventListener(ClickCountEvent.CLICK_COUNT, onItemClickCount);  
				item.setDragEnabled(true);
				item.addEventListener(DragAndDropEvent.DRAG_RECOGNIZED, onDragRecognized);
				
				item.setDropTrigger(true);
				item.setDragAcceptableInitiatorAppraiser(onDropAppraiser);
				item.addEventListener(DragAndDropEvent.DRAG_ENTER, onDragEnter);
				item.addEventListener(DragAndDropEvent.DRAG_EXIT, onDragExit);
				item.addEventListener(DragAndDropEvent.DRAG_DROP, onDragDrop);
				
				//item.addEventListener(MouseEvent.ROLL_OVER, onRollOver);
				//item.addEventListener(MouseEvent.ROLL_OUT, onRollOut);
				item.setBorder(new EmptyBorder());
				item.index = nIndex++;

			}
			nIndex = 0;
			for each (item in _itemsEquip)
			{
				item.addEventListener(ClickCountEvent.CLICK_COUNT, onItemClickCount);  
				item.setDragEnabled(true);
				item.addEventListener(DragAndDropEvent.DRAG_RECOGNIZED, onDragRecognized);
				
				item.setDropTrigger(true);
				item.setDragAcceptableInitiatorAppraiser(onDropAppraiser);
				item.addEventListener(DragAndDropEvent.DRAG_ENTER, onDragEnter);
				item.addEventListener(DragAndDropEvent.DRAG_EXIT, onDragExit);
				item.addEventListener(DragAndDropEvent.DRAG_DROP, onDragDrop);
				
				//item.addEventListener(MouseEvent.ROLL_OVER, onRollOver);
				//item.addEventListener(MouseEvent.ROLL_OUT, onRollOut);
				item.setBorder(new EmptyBorder());
				item.index = nIndex++;
				item.showBackground = false;
				item.enableClick = false;
			}

			for each(var btn:JToggleButton in _btnsPages)
			{
				btn.setDropTrigger(true);
				btn.setDragAcceptableInitiatorAppraiser(onDropAppraiser);
				btn.addEventListener(DragAndDropEvent.DRAG_ENTER, onDragEnter);
				btn.addEventListener(DragAndDropEvent.DRAG_EXIT, onDragExit);
				btn.addEventListener(DragAndDropEvent.DRAG_DROP, onDragDrop);
				btn.addActionListener(onBagTab);
			}
			
			btn_bag_page_1.wrapSimpleButton(Client.getUIElement("btn_bag_page_1") as SimpleButton);
			btn_bag_page_2.wrapSimpleButton(Client.getUIElement("btn_bag_page_2") as SimpleButton);
			btn_bag_page_3.wrapSimpleButton(Client.getUIElement("btn_bag_page_3") as SimpleButton);
			btn_bag_page_4.wrapSimpleButton(Client.getUIElement("btn_bag_page_4") as SimpleButton);
			btn_bag_page_5.wrapSimpleButton(Client.getUIElement("btn_bag_page_5") as SimpleButton);

			btn_bag_page_1.setText("");
			btn_bag_page_2.setText("");
			btn_bag_page_3.setText("");
			btn_bag_page_4.setText("");
			btn_bag_page_5.setText("");
			
			btn_bag_sort.wrapSimpleButton(Client.getUIElement("btn_bag_sort") as SimpleButton);
			btn_bag_sort.setText("");
			
			btn_bag_stall.wrapSimpleButton(Client.getUIElement("btn_bag_stall") as SimpleButton);
			btn_bag_stall.setText("");
			
			btn_bag_tab_all.wrapSimpleButton(Client.getUIElement("btn_bag_tab_all") as SimpleButton);
			btn_bag_tab_all.setText("");
			btn_bag_tab_equip.wrapSimpleButton(Client.getUIElement("btn_bag_tab_equip") as SimpleButton);
			btn_bag_tab_equip.setText("");
			btn_bag_tab_pet.wrapSimpleButton(Client.getUIElement("btn_bag_tab_pet") as SimpleButton);
			btn_bag_tab_pet.setText("");
			btn_bag_tab_props.wrapSimpleButton(Client.getUIElement("btn_bag_tab_props") as SimpleButton);
			btn_bag_tab_props.setText("");
			btn_bag_tab_task.wrapSimpleButton(Client.getUIElement("btn_bag_tab_task") as SimpleButton);
			btn_bag_tab_task.setText("");
			
			btn_bag_tab_all.setSelected(true);
			var groupBagTabs:ButtonGroup = new ButtonGroup();
			groupBagTabs.appendAll(btn_bag_tab_all,btn_bag_tab_equip,btn_bag_tab_pet,btn_bag_tab_props,btn_bag_tab_task);
			
			btn_bag_page_1.setSelected(true);
			groupBagTabs = new ButtonGroup();
			groupBagTabs.appendAll(btn_bag_page_1,btn_bag_page_2,btn_bag_page_3,btn_bag_page_4,btn_bag_page_5);

			updateCharImage();
		}
		static private var _loaderChar:CharImageLoader = null;
		public function updateCharImage():void
		{
			if(_loaderChar == null)
				_loaderChar = new CharImageLoader;
			var arCharEquips:Array = new Array();
			//if(CharInfo.m_nImageId != 0)
			//{
			//	arCharEquips.push(Engine.assetMapMonsterPath + "npc_"+ CharInfo.m_nImageId + '.swf');
			//}
			//else
			//{
				for each(var item:Object in CharInfo.m_arItemPage[0])
				{
					if(item.csv_id != 0)
					{
						var objItemCSV:Object = Client.getInstance().GetItemCSV(item.csv_id);
						arCharEquips.push(objItemCSV.equip_image_id);
					}
					else
					{
						arCharEquips.push(0);
					}
				}
			//}
			_loaderChar.loadChar(CharInfo.m_nRace,CharInfo.m_nGender,arCharEquips,onCharLoaded);
		}
		private var _mcCharHead:MovieClip = null;
		private function onCharLoaded():void
		{
			var imgSelf:JPanel = panelImage;
			if(_mcCharHead != null && imgSelf.contains(_mcCharHead))
			{
				imgSelf.removeChild(_mcCharHead)
				_mcCharHead = null;
			}
			_mcCharHead = _loaderChar.getImage("mc_object_stand",true);
			if(_mcCharHead == null)
				return;
			var mc:MovieClip = _mcCharHead;
			mc.scaleX = panelImage.height/mc.height;
			mc.scaleY = panelImage.height/mc.height;
			mc.x = (imgSelf.width - mc.width)/2;
			if(CharInfo.m_nGender == 0)
				mc.y = Client._headOffsetYBoy*mc.scaleY;
			else
				mc.y = Client._headOffsetYGirl*mc.scaleY;
			imgSelf.setBackgroundDecorator(null);
			//if(imgSelf.getChildAt(0) != null && imgSelf.getChildAt(0) is MovieClip)
			//{
			//	imgSelf.removeChildAt(0);
			//}
			imgSelf.addChild(mc);
			//_loaderChar.clear();
			//_loaderChar = null;
			
		}
		public function setData():void
		{
			labelMoney.setText(CharInfo.money.toString());
			labelIngot.setText(CharInfo.ingot.toString());
			labelWood.setText(CharInfo.wood.toString());
			labelFood.setText(CharInfo.food.toString());
			//updateBag(CharInfo.m_arItemPage[_currentPage]);

			var arr:Array = null;
			if(btn_bag_tab_all.isSelected())
				arr = CharInfo.m_arItemPage[_currentPage];
			if(btn_bag_tab_equip.isSelected())
				arr = getItemsByTypeBagPage([Const.ITEM_TYPE_EQUIP],_currentPage);
			if(btn_bag_tab_pet.isSelected())
				arr = getItemsByTypeBagPage([Const.ITEM_TYPE_PET_EGG,Const.ITEM_TYPE_PET_BOOK],_currentPage);
			if(btn_bag_tab_props.isSelected())
				arr = getItemsByTypeBagPage([Const.ITEM_TYPE_PROPS],_currentPage);
			if(btn_bag_tab_task.isSelected())
				arr = getItemsByTypeBagPage([Const.ITEM_TYPE_TASK],_currentPage);
			updateBag(arr);
			updateEquip();
		}
		static public function hasItem(csv_id:int):Boolean
		{
			for(var i:int = 1; i < 5; i++)
			{
				var array:Array = CharInfo.m_arItemPage[i];
				for each(var item:Object in array)
				{
					if(item.csv_id == csv_id)
					{
						return true;
					}
				}
			}
			return false;
		}
		public static var _currentPage:uint = 1;
		private function onBagTab(event:Event):void
		{
			var target:JToggleButton = event.target as JToggleButton;
			if(target == btn_bag_page_1)
			{
				_currentPage = 1;
			}
			if(target == btn_bag_page_2)
			{
				_currentPage = 2;
			}
			if(target == btn_bag_page_3)
			{
				_currentPage = 3;
			}
			if(target == btn_bag_page_4)
			{
				_currentPage = 4;
			}
			if(target == btn_bag_page_5)
			{
				_currentPage = 5;
			}
			setData();
		}
		private function updateEquip():void
		{
			for each (var panelItem:PanelItem in _itemsEquip)
			{
				panelItem.item = null;
			}
			
			var nIndex:uint = 0;
			for each(var item:Object in CharInfo.m_arItemPage[0])
			{
				_itemsEquip[nIndex].item = item;
				nIndex++;
				if(nIndex >= 20)
					break;
			}
			
		}
		private function updateBag(array:Array):void
		{

			for each (var panelItem:PanelItem in _items)
			{
				panelItem.item = null;
			}
			
			var nIndex:uint = 0;
			for each(var item:Object in array)
			{
				_items[nIndex].item = item;
				nIndex++;
				if(nIndex >= 20)
					break;
			}
			PanelItem.hideClick();
		}
		/*private function onRollOver(event:MouseEvent):void
		{
			var target:PanelItem = event.target as PanelItem;
			if(!(target.getBorder() is EmptyBorder))
				return;
			target.setBorder(new LineBorder(null,ASColor.YELLOW));
		}
		
		private function onRollOut(event:MouseEvent):void
		{
			var target:PanelItem = event.target as PanelItem;
			target.setBorder(new EmptyBorder());
		}*/
		
		//是否接受这个被拖入的组件
		private function onDropAppraiser(initiator:JPanel):Boolean
		{
			if(!btn_bag_tab_all.isSelected())
			{
				btn_bag_tab_all.setSelected(true);
				btn_bag_tab_equip.setSelected(false);
				btn_bag_tab_pet.setSelected(false);
				btn_bag_tab_props.setSelected(false);
				btn_bag_tab_task.setSelected(false);
				setData();
			}
			return _itemsEquip.indexOf(initiator) != -1 || _items.indexOf(initiator) != -1;
		}
		//被接受的组件被拖入时，改变Panel背景色
		private function onDragEnter(e:DragAndDropEvent):void
		{
			var initiator:PanelItem = e.getDragInitiator() as PanelItem;
			if(onDropAppraiser(initiator))
			{
				if(e.target is PanelItem)
				{
					var target:PanelItem = e.target as PanelItem;
					target.setBorder(new LineBorder(null,ASColor.GREEN));
				}
				
				if(e.target is JToggleButton)
				{
					var btn:JToggleButton = e.target as JToggleButton;
					btn.setBorder(new LineBorder(null,ASColor.GREEN));
				}
			}
		}
		
		//组件被拖出时，恢复原来的背景色
		private function onDragExit(e:DragAndDropEvent):void
		{
			if(e.target is PanelItem)
			{
				var target:JPanel = e.target as JPanel;
				target.setBorder(new EmptyBorder());
			}
			if(e.target is JToggleButton)
			{
				var btn:JToggleButton = e.target as JToggleButton;
				btn.setBorder(new EmptyBorder());
			}
		}
		//组件被释放时，如果是被接受的，那么把它加入此面板
		private function onDragDrop(event:DragAndDropEvent):void
		{
			var initiator:PanelItem = event.getDragInitiator() as PanelItem;
			if(event.target is PanelItem)
			{
				var target:PanelItem = event.target as PanelItem;
				target.setBorder(new EmptyBorder());
				if(onDropAppraiser(initiator))
				{
					trace("_currentPage:"+_currentPage);
					trace("initiator.item.page:"+initiator.item.page);
					trace("target.item.page:"+target.item.page);
					
					if(_itemsEquip.indexOf(target) != -1)// put on equip 穿上装备
					{
						Client.getInstance().sendPacketLogic(19,[int(initiator.item.page),int(initiator.item.index),int(target.item.index)]);
						return;
					}
					if(_itemsEquip.indexOf(initiator) != -1)// take off equip 脱下装备
					{
						Client.getInstance().sendPacketLogic(20,[int(initiator.item.index),int(target.item.page),int(target.item.index)]);
						return;
					}
					Client.getInstance().sendPacketLogic(17,[int(initiator.item.page),int(initiator.item.index),int(target.item.page),int(target.item.index)]);
				}
			}
			
			if(event.target is JToggleButton)
			{
				var btn:JToggleButton = event.target as JToggleButton;
				btn.setBorder(new EmptyBorder());
				var _destPage:int = 1;
				if(btn == btn_bag_page_1)
				{
					_destPage = 1;
				}
				if(btn == btn_bag_page_2)
				{
					_destPage = 2;
				}
				if(btn == btn_bag_page_3)
				{
					_destPage = 3;
				}
				if(btn == btn_bag_page_4)
				{
					_destPage = 4;
				}
				if(btn == btn_bag_page_5)
				{
					_destPage = 5;
				}
				if(onDropAppraiser(initiator))
				{
				
					Client.getInstance().sendPacketLogic(17,[int(initiator.item.page),initiator.index,_destPage,-1]);
				}
			}
		}
		/**
		 * 监听到dragBtn的ON_DRAG_RECOGNIZED事件，将拖拽事件托管给DragManager。
		 * @param dragInitiator          此例中是指dragBtn。
		 * @param touchedChild    被鼠标拖住的组件，此例中亦是dragBtn。
		 */
		
		//private var _currentIndex:uint = 0;
		private function onDragRecognized( e:DragAndDropEvent):void
		{
			var btn:PanelItem = e.target as PanelItem;
			if(btn.icon_id <= 0)
				return;
			
			DragManager.startDrag(btn,new SourceData("item", btn.getToolTipText()),new DraggingImageItem(btn)); 
			//_currentIndex = btn.index;
		}
		
		private function onItemClickCount(event:ClickCountEvent):void
		{   
			
			if(event.getCount() >= 2)
			{   
				var target:PanelItem = event.target as PanelItem;
				if(_itemsEquip.indexOf(target) != -1)// take off equip 脱下装备
				{
					Client.getInstance().sendPacketLogic(20,[int(target.item.index),_currentPage,-1]);
					return;
				}
				Client.getInstance().sendPacketLogic(18,[int(target.item.page),int(target.item.index)]);
				//处理  
				trace("onDoubleClickItem"); 
				
			}   
			
		} 
		
		//组件被释放时，如果是被接受的，那么把它加入此面板
		public static function onDragDropPanelMain(event:DragAndDropEvent):void
		{
			trace("onDragDropPanelMain");
			var initiator:PanelItem = event.getDragInitiator() as PanelItem;
			if(!FrameBag.isShowing())
				return;

			if( FrameBag._frame._items.indexOf(initiator) == -1)
				return;
			//Client.getInstance().sendPacketLogic(16,[_currentPage,initiator.index]);
			var objItemCSV:Object = Client.getInstance().GetItemCSV(CharInfo.m_arItemPage[initiator.item.page][initiator.index].csv_id);
			var pane:FrameInfo = FrameInfo.showMessageDialog("","是否丢弃物品 "+objItemCSV.name+" ?",handleOptionRemoveItem,null,true,null,JOptionPane.YES|JOptionPane.NO);
			nPageRemove = initiator.item.page;
			nIndexRemove = initiator.index;
		}
		private static var nPageRemove:uint = 0;
		private static var nIndexRemove:uint = 0;
		private static function handleOptionRemoveItem(button:uint):void
		{
			if(button == JOptionPane.YES)
			{
				Client.getInstance().sendPacketLogic(16,[nPageRemove,nIndexRemove]);
			}
		}

		public static var _frame:FrameBag = null;
		public static function show():void
		{
			if(isShowing())
			{
				hide()
				return;
			}
			if(_frame == null)
			{
				_frame = new FrameBag();
				PanelMainHelp.getInstance().addChild(_frame);
			}
			_frame.show();
			PanelMainHelp.getInstance().bringToTop(_frame);
			_frame.setData();
			_frame.setLocationXY((Engine.W - _frame.width)/2,(Engine.H - _frame.height)/2);
			
		}
		public static function hide():void
		{
			if(isShowing())_frame.hide();
		}
		public static function isShowing():Boolean
		{
			return(_frame != null&& _frame.isShowing());
		}
		private var _needCloseChar:Boolean = false;
		public static function phUpdateItem_15(e:PacketEvent):void
		{
			var msg:Array = e.data as Array;
			trace("phUpdateItem_15", msg);
			var nIndex:uint = 0;
			var nPage:uint = msg[nIndex++];
			var array:Array;
			array = CharInfo.m_arItemPage[nPage];
			var nPageIndex:uint = msg[nIndex++];
			
			do
			{
				var obj:Object;
				if(array[nPageIndex] == null)
				{
					obj = new Object();
					array[nPageIndex] = obj;
				}
				else
					obj = array[nPageIndex];
				obj.csv_id = msg[nIndex++];
				obj.page = nPage;
				obj.index = nPageIndex;
				if(obj.csv_id == 0)
				{
					break;
				}
				obj.count = msg[nIndex++];
				obj.equip_level = msg[nIndex++];
				obj.equip_hp = msg[nIndex++];
				obj.equip_ap = msg[nIndex++];
				obj.equip_dp = msg[nIndex++];
				obj.equip_sp = msg[nIndex++];
				
				obj.equip_enhanceHL = msg[nIndex++];
				obj.equip_enhanceBD = msg[nIndex++];
				obj.equip_enhanceHS = msg[nIndex++];
				
				obj.equip_resistHL = msg[nIndex++];
				obj.equip_resistBD = msg[nIndex++];
				obj.equip_resistHS = msg[nIndex++];
				
				obj.equip_prop_BJ = msg[nIndex++];
				obj.equip_prop_SB = msg[nIndex++];
				obj.equip_prop_FJ = msg[nIndex++];
				obj.equip_prop_LJ = msg[nIndex++];
				
				obj.creator_char_gender = msg[nIndex++];
				obj.creator_char_name = msg[nIndex++];
				obj.creator_lover_gender = msg[nIndex++];
				obj.creator_lover_name = msg[nIndex++];
			}while(0);
			
			if(FrameBag.isShowing())
			{
				FrameBag._frame.setData();
			}
			
			if(nPage == 0)
			{
				if(FrameChar.isShowing())
					FrameChar._frame.onUpdateData();
				PanelMain.getInstance().updatePlayerMainImage();
				
				
				if(!FrameChar.isShowing() && !FrameHomeBuilding9Equip.isShowingEquip() && _frame != null)
				{
					_frame._needCloseChar = true;
					FrameChar.show();
					_frame.setLocationXY((Engine.W - _frame.width)/2,(Engine.H - _frame.height)/2);
					var teamDestX:int = (Engine.W - (_frame.width+FrameChar._frame.width))/2;
					var teamDestY:int = (Engine.H - FrameChar._frame.height)/2;
					
					var listDestX:int = teamDestX + FrameChar._frame.width;
					var listDestY:int = teamDestY + (FrameChar._frame.height - _frame.height)/2;
					
					var tween:TweenLite = TweenLite.to(FrameChar._frame, 0.3, {x:teamDestX, y:teamDestY});
					var list_tween:TweenLite = TweenLite.to(_frame, 0.3, {x:listDestX, y:listDestY});
				}
			}
			FrameHomeBuilding9Equip.update();
		}
		public static function phItems_13(e:PacketEvent):void
		{
			var msg:Array = e.data as Array;
			trace("phItems_13", msg);
			var nIndex:uint = 0;
			var nIndexBag:uint = msg[nIndex++];
			var array:Array = CharInfo.m_arItemPage[nIndexBag];
			array.splice(0,array.length);
			var nCount:uint = msg[nIndex++];
			for(var i:int = 0; i < nCount; i++)
			{
				var obj:Object = new Object();
				array[i] = obj;
				obj.csv_id = msg[nIndex++];
				obj.page = nIndexBag;
				obj.index = i;
				if(obj.csv_id == 0)
				{
					continue;
				}
				obj.count = msg[nIndex++];
				obj.equip_level = msg[nIndex++];
				obj.equip_hp = msg[nIndex++];
				obj.equip_ap = msg[nIndex++];
				obj.equip_dp = msg[nIndex++];
				obj.equip_sp = msg[nIndex++];
				
				obj.equip_enhanceHL = msg[nIndex++];
				obj.equip_enhanceBD = msg[nIndex++];
				obj.equip_enhanceHS = msg[nIndex++];
				
				obj.equip_resistHL = msg[nIndex++];
				obj.equip_resistBD = msg[nIndex++];
				obj.equip_resistHS = msg[nIndex++];
				
				obj.equip_prop_BJ = msg[nIndex++];
				obj.equip_prop_SB = msg[nIndex++];
				obj.equip_prop_FJ = msg[nIndex++];
				obj.equip_prop_LJ = msg[nIndex++];
				
				obj.creator_char_gender = msg[nIndex++];
				obj.creator_char_name = msg[nIndex++];
				obj.creator_lover_gender = msg[nIndex++];
				obj.creator_lover_name = msg[nIndex++];
				
			}
			FrameHomeBuilding9Equip.update();
		}
		public static var arrAddItems:Array = [];
		public static function phAddItem_53(e:PacketEvent):void
		{
			var msg:Array = e.data as Array;
			trace("phAddItem_53", msg);
			var nIndex:uint = 0;
			var nCSVId:uint = msg[nIndex++];
			var nCount:uint = msg[nIndex++];
			var objCSVItem:Object = Client.getInstance().GetItemCSV(nCSVId);
			if(objCSVItem == null)
				return;
			var panel:PanelItemCSV = new PanelItemCSV;
			panel.item = objCSVItem;
			PanelMainHelp.getInstance().addChild(panel);
			panel.setLocationXY((Engine.W - panel.width)/2+panel.width*arrAddItems.length,(Engine.H - panel.height)/2);
			arrAddItems.push(panel);
			var point:IntPoint = PanelMainHelp.getInstance().btn_bag.getLocation();
			var destPoint:Point = PanelMainHelp.getInstance().panelSouthButtons.localToGlobal(new Point(point.x,point.y));
			ease:Bounce.easeOut
			//ease:Back.easeIn
			TweenMax.to(panel, 2, {rotation:360,bezier:[{x:destPoint.x+panel.width*arrAddItems.length, y:destPoint.y-250}, {x:destPoint.x, y:destPoint.y}],ease:Back.easeIn,onComplete:function(pane:JPanel):void
			{
				TweenLite.to(pane, 0.5, {rotation:360,onComplete:function(pane:JPanel):void//autoAlpha:0,
				{
					var nIndex:int = arrAddItems.indexOf(pane);
					if (nIndex != -1)
						arrAddItems.splice(nIndex,1);
					if(PanelMainHelp.getInstance().containsChild(pane))
						PanelMainHelp.getInstance().removeChild(pane);
				},onCompleteParams:[pane]});
			},onCompleteParams:[panel]});
			
		}
		public static function getItemsByTypeEquip(typeArray:Array):Array
		{
			var retArray:Array = [];

			for each(var obj:Object in CharInfo.m_arItemPage[0])
			{
				if(obj.csv_id == 0)
				{
					continue;
				}
				var objItemCSV:Object = Client.getInstance().GetItemCSV(obj.csv_id);
				if(objItemCSV == null)
				{
					continue;
				}
				if(typeArray.indexOf(int(objItemCSV.type)) != -1)
					retArray.push(obj);
			}
			return retArray;
		}
		
		public static function getItemsByTypeBagPage(typeArray:Array,nPage:int):Array
		{
			var retArray:Array = [];
			
			for each(var obj:Object in CharInfo.m_arItemPage[nPage])
			{
				if(obj.csv_id == 0)
				{
					continue;
				}
				var objItemCSV:Object = Client.getInstance().GetItemCSV(obj.csv_id);
				if(objItemCSV == null)
				{
					continue;
				}
				if(typeArray.indexOf(int(objItemCSV.type)) != -1)
					retArray.push(obj);
			}
			return retArray;
		}
		
		public static function getItemsByTypeBag(typeArray:Array):Array
		{
			var retArray:Array = [];
			for (var i:int = 1; i < CharInfo.m_arItemPage.length; i++)
			{
				for each(var obj:Object in CharInfo.m_arItemPage[i])
				{
					if(obj.csv_id == 0)
					{
						continue;
					}
					var objItemCSV:Object = Client.getInstance().GetItemCSV(obj.csv_id);
					if(objItemCSV == null)
					{
						continue;
					}
					if(typeArray.indexOf(int(objItemCSV.type)) != -1)
						retArray.push(obj);
				}
			}
			return retArray;
		}
		public static function getItemsByType(typeArray:Array):Array
		{
			var retArray:Array = [];
			for each(var arr:Array in CharInfo.m_arItemPage)
			{
				for each(var obj:Object in arr)
				{
					if(obj.csv_id == 0)
					{
						continue;
					}
					var objItemCSV:Object = Client.getInstance().GetItemCSV(obj.csv_id);
					if(objItemCSV == null)
					{
						continue;
					}
					if(typeArray.indexOf(int(objItemCSV.type)) != -1)
						retArray.push(obj);
				}
			}
			return retArray;
		}
		
		public static function getItemsByCSVIdEquip(typeArray:Array):Array
		{
			var retArray:Array = [];
			
			for each(var obj:Object in CharInfo.m_arItemPage[0])
			{
				if(obj.csv_id == 0)
				{
					continue;
				}
				if(typeArray.indexOf(int(obj.csv_id)) != -1)
					retArray.push(obj);
			}
			return retArray;
		}
		
		public static function getItemsByCSVIdBagPage(typeArray:Array,nPage:int):Array
		{
			var retArray:Array = [];
			
			for each(var obj:Object in CharInfo.m_arItemPage[nPage])
			{
				if(obj.csv_id == 0)
				{
					continue;
				}
				if(typeArray.indexOf(int(obj.csv_id)) != -1)
					retArray.push(obj);
			}
			return retArray;
		}
		
		public static function getItemsByCSVIdBag(typeArray:Array):Array
		{
			var retArray:Array = [];
			for (var i:int = 1; i < CharInfo.m_arItemPage.length; i++)
			{
				for each(var obj:Object in CharInfo.m_arItemPage[i])
				{
					if(obj.csv_id == 0)
					{
						continue;
					}
					if(typeArray.indexOf(int(obj.csv_id)) != -1)
						retArray.push(obj);
				}
			}
			return retArray;
		}
		public static function getItemsByCSVId(typeArray:Array):Array
		{
			var retArray:Array = [];
			for each(var arr:Array in CharInfo.m_arItemPage)
			{
				for each(var obj:Object in arr)
				{
					if(obj.csv_id == 0)
					{
						continue;
					}
					if(typeArray.indexOf(int(obj.csv_id)) != -1)
						retArray.push(obj);
				}
			}
			return retArray;
		}
	}
}

//import src.*;


